package cc.telecomdigital.tdstock;

import java.util.Vector;

import android.app.Dialog;
import android.content.Context;
import android.view.Window;
import android.widget.TextView;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.view.SunFlowerView;

/**
 * @author ouyangzhihui
 */
public class ProgressDialog extends Dialog
{
	protected TextView						content;
	protected SunFlowerView					sunFlower;
	public static Vector<ProgressDialog>	AllInstance		= new Vector<ProgressDialog>();
	private final static int				ALLOW_MAX_SIZE	= 3;
//	private boolean							isBack			= true;

	private void init()
	{
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.processdialog);

		content = (TextView) findViewById(R.id.content);
		sunFlower = (SunFlowerView) findViewById(R.id.sunflower);
		sunFlower.startAnimation();

		while (AllInstance.size() > ALLOW_MAX_SIZE)
		{
			if (AllInstance.get(AllInstance.size() - 1) != null && AllInstance.get(AllInstance.size() - 1).isShowing())
			{
				AllInstance.get(AllInstance.size() - 1).dismiss();
			}
			AllInstance.remove(AllInstance.size() - 1);
		}
		AllInstance.add(this);
	}

	public ProgressDialog(Context context)
	{
		super(context, R.style.dialog);
		init();
		setCancelable(true);		
	}

	public ProgressDialog(Context context, boolean isBack)
	{
		super(context, R.style.dialog);
		init();
//		this.isBack = isBack;
		setCancelable(isBack);		
	}

	public ProgressDialog(Context context, String theContent)
	{
		super(context, R.style.dialog);
		init();
		content.setText(theContent);
		setCancelable(true);		
	}
	
	public ProgressDialog(Context context, String theContent, boolean isBack)
	{
		super(context, R.style.dialog);
		init();
		content.setText(theContent);
//		this.isBack = isBack;
		setCancelable(isBack);
	}

	public void setMsgContent(String msg)
	{
		content.setText(msg);
		content.postInvalidate();
	}

	public static void dismissAll()
	{
		while (AllInstance.size() > 0)
		{
			if (AllInstance.get(AllInstance.size() - 1) != null && AllInstance.get(AllInstance.size() - 1).isShowing())
			{
				AllInstance.get(AllInstance.size() - 1).dismiss();
			}
			AllInstance.remove(AllInstance.size() - 1);
		}
	}

//	@Override
//	public void onBackPressed()
//	{
//		if(isBack)
//			super.onBackPressed();
//	}
}